<!--
 * @Description: 
 * @Author: 胡涛
 * @Date: 2023-10-15 12:42:16
 * @LastEditors: 胡涛
 * @LastEditTime: 2023-10-24 10:24:10
-->
<template>
  <a-modal
    :open="visible"
    :keyboard="false"
    :maskClosable="false"
    :title="title"
    @cancel="onCancel"
    @ok="onSubmit"
    :confirmLoading="submitLoading"
    :width="800"
  >
    <a-form ref="formRef" v-bind="formLayout" :model="model" :rules="rules">
      <!-- <a-form-item label="所属类别">
        <dictionary-select
          v-model:value="model.groupName"
          name="Platform.OrganizeGroup"
          :disabled="true"
        ></dictionary-select>
      </a-form-item> -->
      <a-form-item label="上级机构">
        <a-tree-select
          :treeData="categoryItems"
          v-model:value="model.parentId"
          @change="onCategroyChanged"
          :disabled="isEdit"
          clear
        >
        </a-tree-select>
      </a-form-item>
      <a-form-item label="名称" prop="name">
        <a-input v-model:value="model.name"> </a-input>
      </a-form-item>
      <a-form-item label="编码" prop="pathCode">
        <a-input
          v-model:value="model.pathCode"
          :disabled="isEdit"
          :addon-before="parentPathCode"
        >
        </a-input>
      </a-form-item>
      <a-form-item label="节点类别" prop="nodeType">
        <enum-select
          v-model:value="model.nodeType"
          name="StructureNodeType"
        ></enum-select>
      </a-form-item>
      <a-form-item label="启用状态">
        <a-switch v-model:checked="model.status"></a-switch>
      </a-form-item>
      <a-form-item label="备注">
        <a-input v-model:value="model.remarks"></a-input>
      </a-form-item>
      <a-form-item label="扩展参数">
        <ExtraPropteries :items="model.extraProperties" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script lang="ts" setup>
import { OrganizationDto } from "@/api/organize/types";
import { useFormPage } from "@/helpers/pages";
import { FormInstance } from "ant-design-vue";
import { computed, onMounted, ref, watch } from "vue";
import api from "@/api/organize/organization";
import { EmptyGuid } from "@/helpers/contants";
import { useAppStore } from "@/store";
const formRef = ref<FormInstance>();
const emits = defineEmits(["update:visible", "close", "success"]);
const appStore = useAppStore();

const props = defineProps({
  id: String,
  visible: { type: Boolean, default: false },
  action: {
    type: String,
    default: "create",
  },
  item: {
    type: Object,
    default() {
      return {};
    },
  },
  categoryItems: { type: Array, required: true },
});

const { model, rules, formLayout, loading, submitLoading } = useFormPage<
  OrganizationDto,
  string
>({
  rules: {
    name: [{ required: true, message: "请输入名称信息" }],
    pathCode: [{ required: true, message: "请输入编码信息" }],
    nodeType: [{ required: true, message: "请选择架构节点类型信息" }],
  },
  model: {
    groupName: "Default",
    parentId: EmptyGuid,
    nodeType: 1,
    name: "",
    pathCode: "",
    enCode: "",
    remarks: "",
    status: true,
    extraProperties: {},
  },
});

const parentPathCode = ref("");

const permissionOptions = ref<any>([]);

onMounted(async () => {
  permissionOptions.value = await appStore.getPermissionDeninitions();
});

const isEdit = computed(() => {
  return props.action === "edit";
});

/**
 * 弹窗标题
 */
const title = computed(() => {
  return isEdit.value ? "修改组织架构信息" : "添加组织架构信息";
});

/**
 * 加载数据
 */
const loadData = () => {
  if (isEdit.value) {
    loading.value = true;
    api.get(props.id).then((res) => {
      Object.assign(model, res);
      loading.value = false;
    });
  }
};

/**
 * 提交
 */
const onSubmit = () => {
  emits("success");
  formRef.value.validate().then(() => {
    let data = { ...model };
    if (!isEdit.value) {
      data.pathCode = parentPathCode.value + data.pathCode;
    }
    let request = isEdit.value ? api.update(props.id, data) : api.create(data);
    submitLoading.value = true;
    request.then((res) => { 
      emits("success", res);
      emits("update:visible", false);
      emits("close");
    })
      .finally(() => {
        submitLoading.value = false;
      });
  });
};

/**
 * 取消
 */
const onCancel = () => {
  emits("update:visible", false);
  emits("close");
};

const onCategroyChanged = (value: any, label: any, extra: any) => {
  let { pathCode } = extra.triggerNode.props;
  parentPathCode.value = pathCode;
};

watch(
  () => props.visible,
  (val: boolean) => {
    console.log("visible changed,val,", val);
    val && loadData();
  },
  { immediate: true }
);

onMounted(() => {
  parentPathCode.value = "";
});
</script>

<style lang="less" scoped></style>
